<template>
  <div class="upload-container">
    <el-button
      :style="{background:color,borderColor:color}"
      icon="el-icon-upload"
      size="small"
      type="primary"
      @click=" dialogVisible=true"
    >
      上传图片
    </el-button>
    <el-dialog
      :title="dialogTitle"
      :visible.sync="dialogVisible"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      append-to-body
    >
      <picture-upload
        :file-list="fileList"
        :limit="limit"
        multiple
        tips="只能上传jpg/png/gif/bmp文件，且不超过2M"
        @change="onChange"
        @remove="handleRemove"
      />
      <div
        slot="footer"
        class="dialog-footer"
      >
        <el-button
          size="small"
          @click="dialogVisible = false"
        >
          取消
        </el-button>
        <el-button
          type="primary"
          size="small"
          @click="handleSubmit"
        >
          保存到当前相册
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import PictureUpload from '../picture'
import { uploadFile } from '@/api/attach'
export default {
  name: 'DialogUpload',
  components: { PictureUpload },
  props: {
    color: {
      type: String,
      default: '#1890ff'
    },
    dialogTitle: {
      type: String,
      default: '图片上传'
    },
    bindParams: {
      type: Object,
      default: null
    },
    limit: {
      type: Number,
      default: 21
    },
    accept: {
      type: [Array, String],
      default: 'image/jpeg, image/gif, image/png, image/bmp'
    }
  },
  provide() {
    return {
      uploadAction: uploadFile,
      uploadOptions: this.uploadOptions
    }
  },
  data() {
    return {
      dialogVisible: false,
      listObj: {},
      files: []
    }
  },
  computed: {
    fileList: {
      get() {
        return this.files
      },
      set(val) {
        console.log('parent needto update filelist', val)
        this.files = [...val]
      }
    },
    uploadOptions() {
      return {
        limit: this.limit,
        accept: this.accept
      }
    }
  },
  methods: {
    checkAllSuccess() {
      return Object.keys(this.listObj).every(item => this.listObj[item].hasSuccess)
    },
    onChange(d) {
      console.log(d)
    },
    handleRemove(file, fileList) {
      this.fileList = [...fileList]
    },
    handleSubmit() {
      console.log(this.fileList)
      const data = Object.assign(
        {
          files: this.fileList,
          album_id: 0
        },
        this.bindParams
      )
      this.$http.attach.uploadToAlbum(data).then(res => {
        this.fileList = []
        this.dialogVisible = false
        this.$emit('success', true)
      })
    },
    handleSuccess(response, file) {
      const uid = file.uid
      const objKeyArr = Object.keys(this.listObj)
      for (let i = 0, len = objKeyArr.length; i < len; i++) {
        if (this.listObj[objKeyArr[i]].uid === uid) {
          this.listObj[objKeyArr[i]].url = response.files.file
          this.listObj[objKeyArr[i]].hasSuccess = true
          return
        }
      }
    },
    handleRemove2(file) {
      const uid = file.uid
      const objKeyArr = Object.keys(this.listObj)
      for (let i = 0, len = objKeyArr.length; i < len; i++) {
        if (this.listObj[objKeyArr[i]].uid === uid) {
          delete this.listObj[objKeyArr[i]]
          return
        }
      }
    },
    beforeUpload(file) {
      const _self = this
      const _URL = window.URL || window.webkitURL
      const fileName = file.uid
      this.listObj[fileName] = {}
      return new Promise((resolve, reject) => {
        const img = new Image()
        img.src = _URL.createObjectURL(file)
        img.onload = function() {
          _self.listObj[fileName] = {
            hasSuccess: false,
            uid: file.uid,
            width: this.width,
            height: this.height
          }
        }
        resolve(true)
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.editor-slide-upload {
  margin-bottom: 20px;
  ::v-deep .el-upload--picture-card {
    width: 100%;
  }
}
</style>
